#!/usr/bin/python
# Filename: main.mi2app

import os
import sys
import shutil
import traceback

import mi2app_utils
from mobile_insight.monitor import OnlineMonitor, OfflineReplayer
# from mobile_insight.analyzer import LtePhyAnalyzer
from lte_phy_analyzer import LtePhyAnalyzer
# from realtime_analyzer import RealtimeAnalyzer

list_logEnabled = [
            'LTE_PHY_PDSCH_Packet',
            'LTE_MAC_DL_Transport_Block',
            'LTE_RLC_DL_AM_All_PDU',
            'LTE_RRC_OTA_Packet',
            'LTE_RRC_Serv_Cell_Info',
            'LTE_RRC_MIB_Packet',
            'LTE_RRC_MIB_Message_Log_Packet',
            'LTE_NAS_ESM_State',
            'LTE_NAS_ESM_OTA_Incoming_Packet',
            'LTE_NAS_ESM_OTA_Outgoing_Packet',
            'LTE_NAS_EMM_State',
            'LTE_NAS_EMM_OTA_Incoming_Packet',
            'LTE_NAS_EMM_OTA_Outgoing_Packet',
            'LTE_PHY_Connected_Mode_Intra_Freq_Meas',
            'LTE_PHY_RLM_Report',
            'LTE_PHY_PUSCH_CSF',
            'LTE_PHY_PDSCH_Stat_Indication'
            ]

try:
    cache_directory  = mi2app_utils.get_cache_dir()
    log_directory    = os.path.join(cache_directory, "mi2log")

    src = OnlineMonitor()
    src.set_skip_decoding(False)
    src.set_log_directory(log_directory)
    src.set_log_cut_size(1000.0)

    # src = OfflineReplayer()
    # # # src.set_input_path("/sdcard/diag_log_20151125_215023.qmdl")
    # src.set_input_path("/sdcard/DRX-ATT.qmdl")
    # # src.set_input_path("/sdcard/WCDMA_RRC_state_sample.qmdl")

    lte_phy_analyzer = LtePhyAnalyzer()
    lte_phy_analyzer.set_source(src)

    # # Haotian's code
    # src.enable_log(list_logEnabled)
    # realtimeAnalyzer = RealtimeAnalyzer()
    # realtimeAnalyzer.set_source(src)


    src.run()

except:
    src.log_error(traceback.format_exc())

# finally:
    # mi2app_utils.detach_thread()
    pass